package cn.controller;



import cn.client.ProductClient;
import cn.client.UserClient;
import cn.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "queryByIdFallBack")
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;

    @Autowired
    private ProductClient productClient;

    @GetMapping("/get/{id}")
    public User getHttp(@PathVariable("id") Integer id){
        String url = "http://user-service/hello/"+id;
        User user = restTemplate.getForObject(url,User.class);
        return user;
    }

    @GetMapping("/get")
    public String get(){
        return productClient.findAll();
    }

    @GetMapping("{id}")
//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "4"),
//            @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "20000"),
//            @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "60")
//    })
    public User queryById(@PathVariable("id") Integer id){
        System.out.println("消费者");
    /*    String url = "http://user-service/hello/"+id;
        String user = restTemplate.getForObject(url,String.class);*/

//        if (id%2==1){
//            int a = 2 / 0;
//        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        return userClient.queryById(id);
    }
    public User queryByIdFallBack(){
        User user = new User();
        user.setName("aihua");
        return user;
    }


}
